package com.geekaca.news.service.impl;

import com.geekaca.news.domain.NewsComment;
import com.geekaca.news.mapper.NewsCommentMapper;
import com.geekaca.news.service.CommentService;
import com.geekaca.news.utils.PageBean;
import com.geekaca.news.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private NewsCommentMapper newsCommentMapper;
    @Override
    public int getTotalComments() {
        return newsCommentMapper.getTotalComments();
    }

    @Override
    public List<NewsComment> getCommentList(int page, int limit) {
        int start =(page-1)*limit;
        int pageSize = limit;

        return newsCommentMapper.getCommentList(start, pageSize);
    }

    @Override
    public int updateComment(int commentId, String replyBody) {
        return newsCommentMapper.updateCommentatorInt(commentId,replyBody);
    }

    @Override
    public int deleteCommentByid(Long id) {
        return newsCommentMapper.commentDelete(id);
    }

    @Override
    public int checkDone(Long id) {
        return newsCommentMapper.updateCheckStatus(id);
    }

    public int addComment(NewsComment comment) {
        return newsCommentMapper.insertSelective(comment);
    }

    @Override
    public PageResult getCommentsPage(PageBean pageBean) {
        // 查询所有评论
        List<NewsComment> comments = newsCommentMapper.findCommentList(pageBean);
        // 查询评论总数
//        int total = newsCommentMapper.getTotalComments(pageBean);
        int total = newsCommentMapper.getCommentCountByNewsId(pageBean.getNewsId());
//        int totalPage = (total-1)/pageBean.getPageSize()+1;

        PageResult pageResult = new PageResult(comments, total, pageBean.getPageSize(), pageBean.getPageNo());
//        pageResult.setTotalPage(totalPage);
        return pageResult;

    }

    @Override
    public int getTotalComentCountByNewsId(Long newsId) {
        return newsCommentMapper.getCommentCountByNewsId(newsId);
    }


}
